﻿using KouDai.Core.Engine;
using System;
using System.Diagnostics;
using System.IO;
using System.Text;
using System.Threading.Tasks;
using Windows.Storage;
using Windows.Storage.Streams;

namespace KouDai.Metro.Helper
{
    public class MetroFileEngine : LocalFileAdapter
    {
        /// <summary>
        /// 
        /// //TODO 去掉父类改用share code
        /// 
        /// 将字符串写入文件中
        /// </summary>
        /// <param name="fileName"></param>
        /// <param name="text"></param>
        public override async Task SaveFileAsync(string fileName, string contents)
        {
            IStorageFolder applicationFolder = ApplicationData.Current.LocalFolder;
            IStorageFile storageFile = await applicationFolder.CreateFileAsync(fileName, CreationCollisionOption.ReplaceExisting);
            using (Stream stream = await storageFile.OpenStreamForWriteAsync())
            {
                byte[] content = Encoding.UTF8.GetBytes(contents);
                await stream.WriteAsync(content, 0, content.Length);
            }
        }


        /// <summary>
        /// 读取制定目录的文件
        /// </summary>
        /// <param name="fileName"></param>
        /// <returns></returns>
        public override async Task<string> LoadStrAsync(string fileName)
        {
            string txtResult;
            IStorageFolder applicationFolder = ApplicationData.Current.LocalFolder;
            IStorageFile storageFile = await applicationFolder.GetFileAsync(fileName);
            IRandomAccessStream accessStream = await storageFile.OpenReadAsync();
            using (Stream stream = accessStream.AsStreamForRead((int)accessStream.Size))
            {
                byte[] content = new byte[stream.Length];
                await stream.ReadAsync(content, 0, (int)stream.Length);
                txtResult = Encoding.UTF8.GetString(content, 0, content.Length);
            }
            return txtResult;
        }

        public override async Task SaveFileAsync(string fileName, byte[] fileBytes)
        {
            IStorageFolder applicationFolder = ApplicationData.Current.LocalFolder;
            IStorageFile storageFile = await applicationFolder.CreateFileAsync(fileName, CreationCollisionOption.OpenIfExists);
            try
            {
                using (Stream stream = await storageFile.OpenStreamForWriteAsync())
                {
                    stream.Seek(0, SeekOrigin.Begin);
                    while (stream.Position < fileBytes.Length)
                    {
                        await stream.WriteAsync(fileBytes, (int)stream.Position, (int)(fileBytes.Length - stream.Position));
                    }
                }
            }
            catch (Exception ex)
            {
                Debug.WriteLine("文件写入错误:" + ex.Message.ToString());
            }
        }

        /// <summary>
        /// 从文件中读取流
        /// </summary>
        /// <param name="fileName"></param>
        /// <returns></returns>
        public override async Task<Stream> LoadFileAsync(string folderName,string fileName)
        {
            IStorageFolder applicationFolder = ApplicationData.Current.LocalFolder;
            var imageFolder = await applicationFolder.GetFolderAsync(folderName);
            IStorageFile storageFile = await imageFolder.GetFileAsync(fileName);
            IRandomAccessStream accessStream = await storageFile.OpenReadAsync();
            return accessStream.AsStreamForRead((int)accessStream.Size);
        }

        public override async Task CreateFolder(string folderName)
        {
            var applicationFolder = ApplicationData.Current.LocalFolder;
            await applicationFolder.CreateFolderAsync(folderName, CreationCollisionOption.OpenIfExists);
        }
    }
}
